import java.util.*;
/**
 *  This class is the main class of the "World of Zuul" application. 
 *  "World of Zuul" is a very simple, text based adventure game.  Users 
 *  can walk around some scenery. That's all. It should really be extended 
 *  to make it more interesting!
 * 
 *  To play this game, create an instance of this class and call the "play"
 *  method.
 * 
 *  This main class creates and initialises all the others: it creates all
 *  rooms, creates the parser and starts the game.  It also evaluates and
 *  executes the commands that the parser returns.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2016.02.29
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Player player;
    private Thing currentThing;
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        parser = new Parser();
        player = new Player();
    }

    /**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room outside, pub, officeBuild, library, teachBuild1, teachBuild2, teachBuild3;
        Room canteen, football_field, hall, manDorm, womanDorm, store, pool, basketball_field, zhongdian;
        Room manDorm_2, manDorm_3, womanDorm_2, womanDorm_3;
        // create the rooms
        outside = new Room("你在校门口,这里已坍塌成废墟，你被困在了校园内。你扫视周遭环境，周围有些根部翻出的烧焦的果树，果实散落在乌黑斑驳的地面上。灰暗的天空与浓雾融在一起，分不清现在是几时，前方主干道被光线影响，朦朦胧胧看不真切。");
        officeBuild = new Room("你路经办公楼，楼下的花坛垮掉了一半，花草夹着泥土淌在地上，四周寂静无声，只有鞋底踩着沙石的沙沙声磨磨索索。办公楼里桌椅歪歪扭扭，纸张散落一地，却没有人。");
        teachBuild1 = new Room("你来到了高一教学楼，穿过昏暗走廊进入一间教室，墙面有着弯曲的裂隙，好似经历过地震。你扫视了四周");
        teachBuild2 = new Room("你来到了高二教学楼，这里的景象与高一教学楼别无二致。");
        teachBuild3 = new Room("你来到了高三教学楼，这里的景象与高二教学楼别无二致。");
        football_field = new Room("你来到了教学楼对面的足球场，绿油油的假草与周围的荒芜格格不入。");
        library = new Room("你拉开玻璃门进入了图书馆，一排排的书架整齐地列着，好像这里没有遭到什么破坏。");
        canteen = new Room("你来到了食堂，地上落着吊顶风扇，打饭窗口也碎了一片。你扫视四周.....");
        store = new Room("你下楼来到了商店，货架倒在地上，里面的商品却不在了。你试图寻找一些食物，仔细地观察了起来。");
        hall = new Room("你来到了报告厅，穹顶的一些结构砸在阶梯座椅上，顶上开的大洞让这里接受了雨的冲刷，看起来很是潮湿。");
        basketball_field = new Room("你来到了篮球场，塑胶球场已脱落了大片，花花绿绿的很是丑陋。");
        manDorm = new Room("你来到男生宿舍，楼前贴着“女生止步”。");
        womanDorm = new Room("女生宿舍,楼前贴着“男生止步”。");
        pool = new Room("游泳池");
        manDorm_2 = new Room("二楼，穿过走廊你进入了一间寝室，迎面窜出一股霉味。破洞的的棉被有老鼠生活的痕迹。");
        manDorm_3 = new Room("三楼，这里的情况和二楼的别无二致。");
        womanDorm_2 = new Room("二楼，你穿过了走廊进入一间寝室，打开门就传来一股水臭和霉味，贴在墙上的壁纸脱落了一角，毫无生气地挂在墙上。");
        womanDorm_3 = new Room("三楼,这里的景象与二楼别无二致。");
        zhongdian = new Room("你落地了，脚踩在了校外的土地上。");
        
        // initialise room exits
        outside.setExit("north", hall);
        outside.setThing(new Thing("Apple",10));
        outside.setThing(new Thing("Banana",15));
        
        
        
        hall.setExit("north", officeBuild);
        hall.setExit("south", outside);
        hall.setThing(new Thing("Apple", 10));
        hall.setThing(new Thing("waste_paper", -1));
        hall.setThing(new Thing("bedsheet", 0));
        
        officeBuild.setExit("north", teachBuild1);
        officeBuild.setExit("south", hall);
        officeBuild.setThing(new Thing("waste_paper", -1));
        
        teachBuild1.setExit("north", teachBuild2);
        teachBuild1.setExit("south", hall);
        teachBuild1.setThing(new Thing("test_paper", -1));
        teachBuild1.setThing(new Thing("textbook", -5));
        
        teachBuild2.setExit("north", teachBuild3);
        teachBuild2.setExit("south", teachBuild2);
        teachBuild2.setThing(new Thing("fiction", -5));
        
        teachBuild3.setExit("north", canteen);
        teachBuild3.setExit("south", teachBuild2);
        teachBuild3.setExit("west", library);
        
        canteen.setExit("down",store);
        canteen.setExit("west", womanDorm);
        canteen.setThing(new Thing("dinner", 50));
        canteen.setThing(new Thing("milk", 15));
        
        store.setExit("up", canteen);
        store.setThing(new Thing("snack", 5));
        store.setThing(new Thing("water", 5));
        store.setThing(new Thing("bread", 15));
        
        womanDorm.setExit("south", library);
        womanDorm.setExit("west", basketball_field);
        womanDorm.setExit("up", womanDorm_2);
        
        womanDorm_2.setExit("up", womanDorm_3);
        womanDorm_2.setExit("down", womanDorm);
        womanDorm_2.setThing(new Thing("bedsheet", 0));
        
        womanDorm_3.setExit("down", womanDorm_2);
        
        basketball_field.setExit("south", pool);
        basketball_field.setExit("east", womanDorm);
        
        pool.setExit("east", manDorm);
        pool.setExit("north", basketball_field);
        
        library.setExit("north", womanDorm);
        library.setExit("east", teachBuild3);
        library.setExit("west", manDorm);
        library.setThing(new Thing("some book", 3));
        
        manDorm.setExit("west", pool);
        manDorm.setExit("east", library);
        manDorm.setExit("up", manDorm_2);
        
        manDorm_2.setExit("up", manDorm_3);
        manDorm_2.setExit("down", manDorm);
        
        manDorm_3.setExit("down", manDorm_2);
        manDorm_3.setExit("outside", zhongdian);
        
        currentRoom = outside;
    }

    /**
     *  Main play routine.  Loops until end of play.
     */
    public void play() 
    {            
        printWelcome();

        // Enter the main command loop.  Here we repeatedly read commands and
        // execute them until the game is over.
                
        boolean finished = false;
        while (! finished) {
            Command command = parser.getCommand();
            finished = processCommand(command);
        }
        System.out.println("感谢你的游玩。  再见。");
    }

    /**
     * Print out the opening message for the player.
     */
    private void printWelcome()
    {
        System.out.println();
        System.out.println("欢迎进入逃出生天游戏");
        System.out.println("请在校园内寻找能帮你逃离的物品吧。");
        System.out.println("有问题请输入‘help’寻求帮助");
        System.out.println();
        System.out.println(currentRoom.getDescription());
        System.out.println();
        currentRoom.checkThing();
        currentRoom.printExits();
    }

    /**
     * Given a command, process (that is: execute) the command.
     * @param command The command to be processed.
     * @return true If the command ends the game, false otherwise.
     */
    private boolean processCommand(Command command) 
    {
        boolean wantToQuit = false;

        if(command.isUnknown()) {
            System.out.println("未知命令");
            return false;
        }
        
        String commandWord = command.getCommandWord();
        
        CommandWord cmd = CommandWord.valueOf(commandWord.toUpperCase());
        
        switch(cmd) {
            case HELP:
                printHelp();
                break;
            case GO:
                player.walk();
                if (!player.isAlive()) {
                    System.out.println("你已经没有体力");
                    return true;
                }
                goRoom(command);
                break;
            case QUIT:
                wantToQuit = quit(command);
                break;
            case EAT:
                eat(command);
                break;
            case PICK:
                pickThing(command);
                break;
            case BAG:
                player.checkBag();
                break;
            default:
        }
        return wantToQuit;
    }
    
    private void eat(Command command) {
        String name = command.getSecondWord();
        Thing thing = currentRoom.getThing(name);
        
        if(!command.hasSecondWord()) {
                System.out.println("请与对象一起输入");
                return;
        }
        
        if(currentRoom.getThing(name) != null) {
            Thing food = currentRoom.getThing(name);
            player.eatInRoom(thing);
            System.out.println("吃掉了：" + food.toString());
            player.check();
            currentRoom.removeThing(name);
            currentRoom.checkThing();
            currentRoom.printExits();
            return;
        }
        else {
            player.eat(command.getSecondWord());
        }
    }

    // implementations of user commands:

    /**
     * Print out some help information.
     * Here we print some stupid, cryptic message and a list of the 
     * command words.
     */
    private void printHelp() 
    {
        System.out.println("看来你遇到了一些问题");
        System.out.println();
        CommandWords.addCommandsList();
        System.out.println("请探索校园的每一处吧");
    }

    /** 
     * Try to go in one direction. If there is an exit, enter
     * the new room, otherwise print an error message.
     */
    private void goRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("去哪儿?");
            return;
        }
        
        String direction = command.getSecondWord();
        Room nextRoom = currentRoom.goNext(direction);
        
        if (nextRoom == null) {
            System.out.println("没有这个出口!");
        }
        else {
            if(!currentRoom.isWin(nextRoom)) {
                currentRoom = nextRoom;
                System.out.println(currentRoom.getDescription());
                currentRoom.checkThing();
                currentRoom.printExits();
                player.check();
            }else if(player.isNess(player.getThing("bedsheet"))) {
                System.out.println("你把床单撕成三份，将他们连结成一条绳子，你从三楼滑了下去。");       
                System.out.println("你自由了！！");
                System.out.println("感谢你的游玩。");
                System.exit(0);
                
            }else {
                System.out.println("你从三楼摔了下去,你死了。");
                System.exit(0);
            }
            
        } 
    }
       
    private void pickThing(Command command)
    {
        
        String name = command.getSecondWord();
        if(!command.hasSecondWord()) {
            if (currentRoom.getThing(name) == null) {
                System.out.println("请与对象一起输入");
                return;
            }
        }
        else if(currentRoom.getThing(name) == null) {
            System.out.println("这里没有可以捡的东西");
            return;
        }
        player.pick(currentRoom.getThing(name));
        currentRoom.removeThing(name);
    }
    
    /** 
     * "Quit" was entered. Check the rest of the command to see
     * whether we really quit the game.
     * @return true, if this command quits the game, false otherwise.
     */
    private boolean quit(Command command) 
    {
        if(command.hasSecondWord()) {
            System.out.println("只需“quit”");
            return false;
        }
        else {
            return true;  // signal that we want to quit
        }
    }
    
}
